<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control @tabClick="tabClick" ref="tabControl1" class="tab-control" v-show="fixedTabControl"></tab-control>
    <scroll class="content" ref="scroll" :probeType="3" :pullUpLoad="true" @scroll="contentScroll"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoad"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control @tabClick="tabClick" ref="tabControl2" v-show="!fixedTabControl"></tab-control>
      <goods-list :goods="showGoodsList"></goods-list>
    </scroll>
    <back-top @click.native="backTop" v-show="showBackTop"></back-top>
  </div>
</template>

<script>
  import HomeSwiper from "./childComps/HomeSwiper";
  import RecommendView from "./childComps/RecommendView";
  import FeatureView from "./childComps/FeatureView";

  import NavBar from "components/common/navbar/NavBar";
  import TabControl from "components/content/tabControl/TabControl";
  import GoodsList from "components/content/goods/GoodsList";
  import Scroll from "components/common/scroll/Scroll";
  import BackTop from "components/content/backTop/BackTop";

  import {getMultiData, getProductData} from "network/home";
  import {itemListenerMixin, backTopMixin} from "common/mixin";

  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    mixins: [itemListenerMixin,backTopMixin],
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          pop: {page: 1, list: []},
          new: {page: 1, list: []},
          sell: {page: 1, list: []},
        },
        currentType: 'pop',
        tabOffsetTop: 0,
        fixedTabControl: false,
        saveY: 0,
        itemImgListener: null
      };
    },
    computed: {
      showGoodsList() {
        return this.goods[this.currentType].list;
      },
    },
    created() {
      // 1.请求轮播等数据
      this._getMultiData();

      // 2.请求商品数据
      this._getProductData('pop');
      this._getProductData('new');
      this._getProductData('sell');

    },
    mounted() {

    },
    destroyed() {
      console.log('home destroyed')
    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      // 1.保存Y值
      this.saveY = this.$refs.scroll.getScrollY()
      // 2.取消全局事件的监听
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    },
    methods: {
      /**
       * 网络请求
       */
      _getMultiData() {
        getMultiData().then((res) => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        });
      },
      _getProductData(type) {
        // 获取页码
        const page = this.goods[type].page;
        getProductData(type, page).then((res) => {
          const newList = res.data.list;
          this.goods[type].list.push(...newList);
          this.goods[type].page += 1;
          this.$refs.scroll.scroll.finishPullUp()
        });
      },
      /**
       * 事件监听
       */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop';
            break;
          case 1:
            this.currentType = 'new';
            break;
          case 2:
            this.currentType = 'sell';
            break;
        }
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl2.currentIndex = index
      },
      contentScroll(position) {
        // 1.决定BackTop是否显示
        this.showBackTop = (-position.y) > 1000
        // 2.决定tabControl是否吸顶
        this.fixedTabControl = (-position.y) > this.tabOffsetTop
      },

      loadMore() {
        this._getProductData(this.currentType)
      },

      swiperImgLoad() {
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
      }
    },
  };
</script>

<style scoped>
  #home {
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;
    position: relative;
    z-index: 9;
  }

  .content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }
</style>
